import Vue from 'vue'
import Router from 'vue-router'
import http from '@/services/http'
import iView from 'iview'
import 'iview/dist/styles/iview.css'
import 'bootstrap/dist/css/bootstrap.css'

import home from './home'
import Login from '@/components/user/Login'
import Auth from '@/services/auth'

Vue.prototype.$http = http
Vue.use(Router)
Vue.use(iView)

const router = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/user/login',
      name: 'login',
      component: Login
    },
    ...home
  ]
})

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth && !Auth.authenticated()) {
    next({
      path: '/user/login',
      query: {redirect: to.fullPath}
    })
  } else {
    next()
  }
})

export default router
